package com.hy.lightning.commons.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;

public class ObjectUtil {

	public static boolean isNullOrEmpty(String ...strs) {
		for(String s: strs) {
			if(isNullOrEmpty(s)) return true;
		}
		return false;
	}
	
	public static boolean isNullOrEmpty(String str) {
		return str == null || "".equals(str.trim());
	}
	
	public static boolean isNullOrEmpty(Map<?,?> map) {
		return map == null || map.isEmpty();
	}
	
	public static boolean isNullOrEmpty(List<?> list) {
		return list == null || list.isEmpty();
	}
	
	public static boolean isNullOrEmpty(Object[] list) {
		return list == null || list.length==0;
	}
	
	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
			// 序列化
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (oos != null)
					oos.close();
				if (baos != null)
					baos.close();
			} catch (IOException e) {
			}
		}
		return null;
	}

	public static Object unserialize(byte[] bytes) {
		ByteArrayInputStream bais = null;
		ObjectInputStream ois = null;
		try {
			// 反序列化
			bais = new ByteArrayInputStream(bytes);
			ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bais != null)
					bais.close();
				if (ois != null)
					ois.close();
			} catch (IOException e) {}
		}
		return null;
	}
}
